// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.network.packets.hit;

import java.io.IOException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import zombie.characters.IsoLivingCharacter;
import zombie.core.network.ByteBufferWriter;
import zombie.core.raknet.UdpConnection;
import zombie.debug.DebugLog;
import zombie.debug.LogSeverity;
import zombie.inventory.InventoryItem;
import zombie.inventory.InventoryItemFactory;
import zombie.inventory.types.HandWeapon;
import zombie.network.packets.INetworkPacket;

public class Weapon extends Instance implements INetworkPacket {
    protected InventoryItem item;
    protected HandWeapon weapon;

    public void set(HandWeapon _weapon) {
        super.set(_weapon.getRegistry_id());
        this.item = _weapon;
        this.weapon = _weapon;
    }

    public void parse(ByteBuffer b, IsoLivingCharacter character) {
        boolean _boolean = b.get() == 1;
        if (_boolean) {
            this.ID = b.getShort();
            b.get();
            if (character != null) {
                this.item = character.getPrimaryHandItem();
                if (this.item == null || this.item.getRegistry_id() != this.ID) {
                    this.item = InventoryItemFactory.CreateItem(this.ID);
                }

                if (this.item != null) {
                    try {
                        this.item.load(b, 195);
                    } catch (BufferUnderflowException | IOException iOException) {
                        DebugLog.Multiplayer.printException(iOException, "Weapon load error", LogSeverity.Error);
                        this.item = InventoryItemFactory.CreateItem("Base.BareHands");
                    }
                }
            }
        } else {
            this.item = InventoryItemFactory.CreateItem("Base.BareHands");
        }

        if (character != null) {
            this.weapon = character.bareHands;
            if (this.item instanceof HandWeapon) {
                this.weapon = (HandWeapon)this.item;
            }
        }
    }

    @Override
    public void parse(ByteBuffer b, UdpConnection connection) {
        DebugLog.Multiplayer.error("Weapon.parse is not implemented");
    }

    @Override
    public void write(ByteBufferWriter b) {
        if (this.item == null) {
            b.putByte((byte)0);
        } else {
            b.putByte((byte)1);

            try {
                this.item.save(b.bb, false);
            } catch (IOException iOException) {
                DebugLog.Multiplayer.printException(iOException, "Item write error", LogSeverity.Error);
            }
        }
    }

    @Override
    public boolean isConsistent() {
        return super.isConsistent() && this.weapon != null;
    }

    @Override
    public String getDescription() {
        return super.getDescription() + "\n\tWeapon [ weapon=" + (this.weapon == null ? "?" : "\"" + this.weapon.getDisplayName() + "\"") + " ]";
    }

    HandWeapon getWeapon() {
        return this.weapon;
    }
}
